package com.tgy.cs;

import com.tgy.pojo.Man;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/***
 * @ClassName: SessionDemo1
 * @Description: 学习Session
 * @Auther: 送一一块二向箔
 * @Date: 11:47 2021/12/5
 * @version : V1.0
 */
public class SessionDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        /*得到Session*/
        HttpSession session = req.getSession();
        /*给Session存东西*/
        session.setAttribute("name","二向箔");
        /*存入对象实验*/
        session.setAttribute("pojo",new Man("aaa",6));
        //获取session ID
        String id = session.getId();
        //判断Session是否是新的
        if (session.isNew()) {
            resp.getWriter().write("Session创建成功ID："+id);
        }else {
            resp.getWriter().write("Session已经存在ID："+id);
        }

        //session创建的时候做了什么事情
        //Cookie cookie = new Cookie("JSESSIONID", id);
        //resp.addCookie(cookie);
    }
}
